package com.garden.rocketmq.learn.demo.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 消费者 消费信息
 * Created by Garden on 2020/7/22.
 */
public class FilterConsumer {

    public static void main(String[] args) throws MQClientException {
        //实例化消费者Consumer
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("filter_group");
        //设置NameServer的地址，Consumer启动后通过NameServer获取目标Topic的路由信息
        consumer.setNamesrvAddr("localhost:9876");
        //订阅消息
        //订阅多个Topic，filter_producer_tag_a与filter_producer_tag_b
        consumer.subscribe("test_topic","filter_producer_tag_a || filter_producer_tag_b");
        //订阅符合SQL92规则的消息属性，property = 1
        consumer.subscribe("test_topic", MessageSelector.bySql("property = 1"));
        //注册回调实现类来处理从broker拉取回来的消息
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
                // 标记该消息已经被成功消费
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //启动Consumer实例
        consumer.start();
        System.out.printf("Consumer Started.%n");
    }

    //RocketMQ SQL语法 => 属性key + 关键字 + 常量，如 property = 1
    //关键字
      //数值比较，比如：>，>=，<，<=，BETWEEN，=；
      //字符比较，比如：=，<>，IN；
      //IS NULL 或者 IS NOT NULL；
      //逻辑符号 AND，OR，NOT；
    //常量
      //数值，比如：123，3.1415；
      //字符，比如：'abc'，必须用单引号包裹起来；
      //NULL，特殊的常量
      //布尔值，TRUE 或 FALSE

}
